package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.entity.Manager;
import com.example.demo.service.ManagerService;
import com.example.demo.controller.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/manager")
public class ManagerController {


    @Autowired
    private ManagerService managerService;

    @Autowired
    private RoleService roleService;

    @PostMapping
    public ResponseResult<Manager> createManager(@RequestParam String username,
                                            @RequestParam String password,
                                            @RequestParam Integer role_id,
                                            @RequestParam Integer status,
                                            @RequestParam(required = false) String avatar) {
        log.info("Creating new manager: username={}, role_id={}, status={}", username, role_id, status);
        Manager manager = new Manager();
        manager.setUsername(username);
        manager.setPassword(password);
        manager.setRole_id(role_id);
        manager.setStatus(status);
        manager.setAvatar(avatar);
        Manager createdManager = managerService.createManager(manager);
        log.info("Manager created successfully: id={}", createdManager.getId());
        return ResponseResult.success(createdManager);
    }

    @PostMapping("/{id}")
    public ResponseResult<Manager> updateManager(@PathVariable Integer id,
                                            @RequestParam String username,
                                            @RequestParam String password,
                                            @RequestParam Integer role_id,
                                            @RequestParam Integer status,
                                            @RequestParam String avatar) {
        log.info("Updating manager: id={}, username={}, role_id={}, status={}", 
            id, username, role_id, status);
        Manager manager = new Manager();
        manager.setId(id);
        manager.setUsername(username);
        manager.setPassword(password);
        manager.setRole_id(role_id);
        manager.setStatus(status);
        manager.setAvatar(avatar);
        Manager updatedManager = managerService.updateManager(manager);
        log.info("Manager updated successfully: id={}", updatedManager.getId());
        return ResponseResult.success(updatedManager);
    }

    @PostMapping("/{id}/delete")
    public ResponseResult<Boolean> deleteManager(@PathVariable("id") Integer id) {
        log.info("Deleting manager: id={}", id);
        boolean result = managerService.deleteManager(id);
        if (result) {
            log.info("Manager deleted successfully: id={}", id);
        } else {
            log.warn("Failed to delete manager: id={}", id);
        }
        return ResponseResult.success(result);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getManagerList(@PathVariable Integer page,
                                              @RequestParam(defaultValue = "10") Integer limit,
                                              @RequestParam(required = false) String keyword) {
        log.info("Fetching manager list: page={}, limit={}, keyword={}", page, limit, keyword);
        Object result = managerService.getManagerList(page, limit, keyword);
        log.debug("Fetched {} managers", result instanceof List ? ((List<?>) result).size() : "unknown");
        return ResponseResult.success(result);
    }

    @GetMapping("/common_data")
    public ResponseResult<?> getManagerCommonData() {
        log.info("Fetching manager common data");
        Map<String, Object> commonData = new HashMap<>();
        List<Map<String, Object>> result =  roleService.getAllActiveRoles();
        commonData.put("roles",result);
        log.debug("Fetched manager common data: roles: {}", result != null ? result.size() : "unknown");
        return ResponseResult.success(commonData);
    }

    @PostMapping("/{id}/update_status")
    public ResponseResult<Boolean> updateManagerStatus(@PathVariable Integer id,
                                                 @RequestParam Integer status) {
        log.info("Updating manager status: id={}, new_status={}", id, status);
        boolean result = managerService.updateManagerStatus(id, status);
        if (result) {
            log.info("Manager status updated successfully: id={}", id);
        } else {
            log.warn("Failed to update manager status: id={}", id);
        }
        return ResponseResult.success(result);
    }
}
